﻿//
//  mysql.cpp
//  TcpServer
//
//  Created by sandrial on 2017/9/20.
//  Copyright © 2017年 wzy. All rights reserved.
//

#include "mysql.hpp"
#include <stdio.h>

MysqlDb::MysqlDb(){
    user = "wzy";
    pswd = "1234";
    host = "localhost";
    database = "sqq";
    port = 3306;
}

int MysqlDb::conndb(){
    mysql_init(&db);
    
    if(!mysql_real_connect(&db, host, user, pswd, database, port, NULL, 0)){
        printf("无法连接到数据库\n");
        return -1;
    }
    mysql_query(&db, "SET NAMES UTF-16"); //设置编码格式
    
    return 0;
}

int MysqlDb::disconndb(){
    
    mysql_close(&db);
    return 0;
}

int MysqlDb::dbLogin(std::string &user, std::string& password){
    using namespace std;
    MYSQL_RES* res;
    MYSQL_ROW row;
    
    string sql = "select passwd from users where id = \"";
    sql += user;
	sql += "\"";
    mysql_real_query(&db, sql.c_str(), sql.size());
    
    res = mysql_store_result(&db);
    
    if(!res)
        return 0;
    
    row  = mysql_fetch_row(res);
    if(!row)
        return 0;
    
    if(strcmp(row[0], password.c_str()) != 0){
        return 0;
    }
    
    return 1;
}
